package com.ruoyi.web.controller.medicine;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MedStockRecord;
import com.ruoyi.system.service.IMedStockRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 药品库存改变记录Controller
 * 
 * @author yangxp
 * @date 2020-08-07
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/medicine/medStockRecord" )
public class MedStockRecordController extends BaseController {

    private final IMedStockRecordService iMedStockRecordService;

    /**
     * 查询药品库存改变记录列表
     */
    @PreAuthorize("@ss.hasPermi('medicine:medStockRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(MedStockRecord medStockRecord)
    {
        startPage();
        QueryWrapper<MedStockRecord> lqw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(medStockRecord.getMedName())){
            lqw.like("med_name" ,medStockRecord.getMedName());
        }
        lqw.orderByAsc("med_name").orderByDesc("gmt_create");
        List<MedStockRecord> list = iMedStockRecordService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出药品库存改变记录列表
     */
    @PreAuthorize("@ss.hasPermi('medicine:medStockRecord:export')" )
    @Log(title = "药品库存改变记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(MedStockRecord medStockRecord) {
        LambdaQueryWrapper<MedStockRecord> lqw = new LambdaQueryWrapper<MedStockRecord>(medStockRecord);
        List<MedStockRecord> list = iMedStockRecordService.list(lqw);
        ExcelUtil<MedStockRecord> util = new ExcelUtil<MedStockRecord>(MedStockRecord. class);
        return util.exportExcel(list, "medStockRecord" );
    }

    /**
     * 获取药品库存改变记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('medicine:medStockRecord:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iMedStockRecordService.getById(id));
    }

    /**
     * 新增药品库存改变记录
     */
    @PreAuthorize("@ss.hasPermi('medicine:medStockRecord:add')" )
    @Log(title = "药品库存改变记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MedStockRecord medStockRecord) {
        return toAjax(iMedStockRecordService.save(medStockRecord) ? 1 : 0);
    }

    /**
     * 修改药品库存改变记录
     */
    @PreAuthorize("@ss.hasPermi('medicine:medStockRecord:edit')" )
    @Log(title = "药品库存改变记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MedStockRecord medStockRecord) {
        return toAjax(iMedStockRecordService.updateById(medStockRecord) ? 1 : 0);
    }

    /**
     * 删除药品库存改变记录
     */
    @PreAuthorize("@ss.hasPermi('medicine:medStockRecord:remove')" )
    @Log(title = "药品库存改变记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iMedStockRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
